package com.jackstraw.dbtool.utils;

/**
 * 所在包：com.wenhe.backTemplate.utils
 * 文件名：
 * 版权：稻草人.
 * 项目：backTemplate
 * 功能描述：
 * 修改人：wenhe
 * 修改时间：2019-11-05 14:25
 */
public class MyStringUtil {

    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null) {
            return true;
        } else {
            if ("".equals(str.trim())) {
                return true;
            }
            return false;
        }
    }

    /**
     * 判断字符串是否不为空
     * @param str
     * @return
     */
    public static boolean isNotEmty(String str) {
        if (str == null) {
            return false;
        } else {
            if ("".equals(str.trim())) {
                return false;
            }
            return true;
        }
    }

    /**
     * 驼峰转下划线（首字母默认小写）
     * @param str
     * @return
     */
    public static String transCamelToUnderline(String str) {
        char[] chars = str.toCharArray();
        int length = chars.length;
        String underlineStr = "";
        for (int i=0; i< length; i++) {
            char current = chars[i];
            String currentStr = current + "";
            if (i==0) {
                underlineStr = current + "";
                underlineStr = underlineStr.toLowerCase();
            } else {
                if (current >= 65 && current <= 90) {
                    underlineStr = underlineStr + "_" + currentStr.toLowerCase();
                } else {
                    underlineStr = underlineStr + currentStr;
                }
            }
        }
        return underlineStr;
    }

    /**
     * 将下划线转换成驼峰命名规范（非下划线部分默认全部转小写）
     * @param str
     * @return
     */
    public static String transUnderlineToCamel(String str) {
        if (isNotEmty(str)) {
            String[] strList = str.split("_");
            String camelStr = "";
            int l = strList.length;
            for (int i = 0; i< l; i++) {
                String currentStr = strList[i];
                currentStr = currentStr.trim().toLowerCase();
                if (i>0) {
                    camelStr = camelStr + uperFirstCase(currentStr);
                } else {
                    camelStr = currentStr;
                }
            }
            return camelStr;
        } else {
            return "";
        }
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String uperFirstCase(String str) {
        if (isNotEmty(str)) {
            String firstLetters = str.substring(0,1);
            if (str.length() > 1) {
                return firstLetters.toUpperCase() + str.substring(1);
            } else {
                return firstLetters.toUpperCase();
            }
        } else {
            return "";
        }
    }

    /**
     * 首字母小写
     * @param str
     * @return
     */
    public static String lowerFirstCase(String str) {
        if (isNotEmty(str)) {
            String firstLetters = str.substring(0,1);
            if (str.length() > 1) {
                return firstLetters.toLowerCase() + str.substring(1);
            } else {
                return firstLetters.toLowerCase();
            }
        } else {
            return "";
        }
    }

    /**
     * 获取换行符
     * @return
     */
    public static String changeLine() {
        return "\n";
    }

    public static void main(String[] args) {
        String name = "repertory_order_detail";
        name = transUnderlineToCamel(name);
        name = transCamelToUnderline(name);
        System.out.println(name);
    }
}
